package photo.navi;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryAdapter extends BaseAdapter {
	private Context grid_context;
	
    public GalleryAdapter(Context c){
        grid_context = c;
    }
	public int getCount() {
    	LocationDatabaseManager db = new LocationDatabaseManager(grid_context);
		return db.getPhotoLocationsCount();
	}

	public Object getItem(int index) {
		LocationDatabaseManager db = new LocationDatabaseManager(grid_context);
		List<PhotoLocation> locations = db.getAllPhotoLocations();
		PhotoLocation location = locations.get(index);
		return location;
	}

	public long getItemId(int arg0) {
		return 0;
	}

	public View getView(int index, View arg1, ViewGroup arg2) {
		LocationDatabaseManager db = new LocationDatabaseManager(grid_context);
		List<PhotoLocation> locations = db.getAllPhotoLocations();
		final PhotoLocation location = locations.get(index);
		Bitmap picture = location.picture();
		ImageView imageView = new ImageView(grid_context);
        imageView.setImageBitmap(picture);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        imageView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 Toast.makeText(v.getContext(), "Description: " + location.getDescription(), Toast.LENGTH_LONG).show();
			}
		});
        
        imageView.setOnLongClickListener(new OnLongClickListener() {
			
			public boolean onLongClick(View v) {
				Context context = v.getContext();
				Toast.makeText(context, "Navigating to: " + location.getGpsLocation(), Toast.LENGTH_LONG).show();
				Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr="+location.getGpsLocation()));
				context.startActivity(intent);
				return true;
			}
		});
        return imageView;
	}

}
